from django.shortcuts import render
from rest_framework.views import APIView
from django_redis import get_redis_connection
from rest_framework.permissions import IsAuthenticated
from decimal import Decimal
from rest_framework.response import Response
from rest_framework.generics import CreateAPIView

from goods.models import SKU
from .serializers import OrderSettlementSerializer, CommitOrderSerializer


# Create your views here.
class OrderSettlementView(APIView):

    # 指定权限
    permission_classes = [IsAuthenticated]
    """去结算"""
    def get(self, request):

        user = request.user
        # 获取出redis中的hash字典及set
        redis_conn = get_redis_connection('cart')
        redis_cart = redis_conn.hgetall('cart_%s' % user.id)
        redis_selected = redis_conn.smembers('selected_%s' % user.id)

        # 只拿到勾选商品sku_id 和count

        cart_dict = {}
        # 遍历set集合
        for sku_id_bytes in redis_selected:
            cart_dict[int(sku_id_bytes)] = int(redis_cart[sku_id_bytes])

        # 获取出所有勾选商品的SKU模型
        skus = SKU.objects.filter(id__in=cart_dict.keys())

        # 给对应的每个sku模型多添加一个count属性
        for sku in skus:
            sku.count = cart_dict[sku.id]

        # 运算
        freight = Decimal('10.00')

        # 创建序列化器进行序列化
        """
        序列化器的第一个参数: (单个模型对象, 字典) (查询集, 列表(模型),传这两个参数时,要指定many=True)
        """
        serializer = OrderSettlementSerializer({'freight': freight, 'skus': skus})
        # 响应
        return Response(serializer.data)


class CommitOrderView(CreateAPIView):
    """保存订单"""
    # 指定序列化器
    serializer_class = CommitOrderSerializer
    # 指定权限
    permission_classes = [IsAuthenticated]